package com.dongway.netcondition.web;

import java.net.Socket;
import java.sql.Timestamp;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dongway.core.common.ValidationException;
import com.dongway.core.web.AbstractBaseController;
import com.dongway.netcondition.dto.AutoCompleteItem;
import com.dongway.netcondition.model.DevInfo;
import com.dongway.netcondition.model.OperationRecord;
import com.dongway.netcondition.service.DevSelectHelper;
import com.dongway.netcondition.service.DevService;
import com.dongway.netcondition.service.OperationRecordService;
import com.dongway.netcondition.socketToBox.GetDevIsListeren;
import com.dongway.netcondition.socketToBox.GetIpPhone;
import com.dongway.netcondition.socketToBox.GetNetWork;
import com.dongway.netcondition.socketToBox.SetDevIsListeren;
import com.dongway.netcondition.socketToBox.SetIpPhone;
import com.dongway.netcondition.socketToBox.SetNetwork;
import com.dongway.netcondition.socketToBox.SocketContent;

@Controller
@RequiresRoles("admin")
public class DevController extends AbstractBaseController {

    @Autowired
    private DevService devService;
    @Autowired
    private DevSelectHelper devSelectHelper;
    @Autowired
    private OperationRecordService operationRecordService;


    @RequestMapping("/configure.htm")
    @RequiresRoles("admin")
    public String configure(Integer id, ModelMap modelMap) {
        List<AutoCompleteItem> branches = devSelectHelper.getAllBranch();
        modelMap.put("branches", branches);
        return "net/configure";
    }


    @RequestMapping("/devInfo.json")
    @ResponseBody
    public Object devInfo(Integer stationId) {
        try {
            DevInfo info = devService.searchByStationId(stationId);

            return JsonSuccess(info);
        } catch (Exception e) {
            logger.error("获取信息错误", e);
            return JsonFailed(e);
        }
    }


    /**
     * @param id
     * @return
     */
    @RequestMapping("/checkDevInfo.json")
    @ResponseBody
    public Object checkDevInfo(Integer id, Integer type) {
        try {
            DevInfo info = devService.searchById(id);
            Socket socket = SocketContent.getInstance().getSocket(id);

            OperationRecord operationRecord = new OperationRecord();
            operationRecord.setTime(new Timestamp(System.currentTimeMillis()));

            switch (type) {
            case 0:
                if (socket != null) {
                    info = GetIpPhone.getIpPhone(id);
                    devService.update(info);
                    socket.close();
                    operationRecord.setDevId(id);;
                    operationRecord.setOperation("查询检测与上报信息");

                } else
                    throw new ValidationException("设备没在线，请检测设备是否连接！");
                break;
            case 1:
                if (socket != null) {
                    info = GetNetWork.getInfor(id);
                    devService.update(info);
                    socket.close();
                    operationRecord.setDevId(id);;
                    operationRecord.setOperation("查询设备网卡信息");
                } else
                    throw new ValidationException("设备没在线，请检测设备是否连接！");
                break;
            case 2:
                if (socket != null) {
                    info = GetDevIsListeren.getDevIsListeren(id);
                    devService.update(info);
                    operationRecord.setDevId(id);;
                    operationRecord.setOperation("其他设备配置查询");
                } else
                    throw new ValidationException("设备没在线，请检测设备是否连接！");
                break;

            default:
                break;
            }
            operationRecordService.save(operationRecord);
            return JsonSuccess(info);
        } catch (ValidationException ve) {
            logger.error(ve.getMessage());
            return JsonFailed(ve);
        } catch (Exception e) {
            logger.error("获取信息错误", e);
            return JsonFailed(e);
        }
    }


    @RequiresRoles("admin")
    @RequestMapping("/setInfo.json")
    @ResponseBody
    public Object setConf(DevInfo devInfo, Integer type) {
        try {

            // DevInfo info = devService.searchById(devInfo.getId());
            Socket socket = SocketContent.getInstance().getSocket(devInfo.getId());
            OperationRecord operationRecord = new OperationRecord();
            operationRecord.setTime(new Timestamp(System.currentTimeMillis()));

            String str = "";
            switch (type) {
            case 0:
                if (socket != null) {
                    SetIpPhone.setIpPhone(devInfo);
                    devService.update(devInfo);
                    operationRecord.setDevId(devInfo.getId());
                    operationRecord.setOperation("设置检测与上报信息,设备重启");
                    operationRecordService.save(operationRecord);
                    str = "设置成功，设备即将重启！";
                } else
                    throw new ValidationException("设备没在线，请检测设备是否连接！");
                break;
            case 1:
                if (socket != null) {
                    SetNetwork.setNetwork(devInfo);
                    socket.close();
                    devService.update(devInfo);
                    operationRecord.setDevId(devInfo.getId());
                    operationRecord.setOperation("设置网卡信息,设备重启");
                    operationRecordService.save(operationRecord);
                    str = "设置成功，设备即将重启！";
                } else
                    throw new ValidationException("设备没在线，请检测设备是否连接！");
                break;
            case 2:
                if (socket != null) {
                    SetDevIsListeren.setDevIsListeren(devInfo);
                    devService.update(devInfo);
                    operationRecord.setDevId(devInfo.getId());
                    operationRecord.setOperation("其他设备信息设置");
                    operationRecordService.save(operationRecord);
                    str = "设置成功，立即生效";
                } else
                    throw new ValidationException("设备没在线，请检测设备是否连接！");
                break;
            default:
                break;
            }

            return JsonSuccess(str);
        } catch (ValidationException ve) {
            logger.error(ve.getMessage());
            return JsonFailed(ve);
        } catch (Exception e) {
            logger.error("查询信息错误", e);
            return JsonFailed(e);
        }
    }
}
